import { updateWayBillTransferNoApi } from "@/api/waybillManagement/customerServiceWaybill/index";
import { Modal, message } from "ant-design-vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { Ref, createVNode } from "vue";
import { cancelCombinedApi, combinedDeclarationApi } from "@/api/waybillManagement/businessWaybill/waybillQuotation";
import FormHook from "@/hook/formHook";

export default function OperationHook(state: {
  passData: Ref<Array<WaybillQuotation.ListRows>>;
  type: Ref<number>;
  formState: { billingWaybillNum: string; waybillNumList: Array<string> };
  loading: Ref<boolean>;
  formRef: any;

  emit: {
    (e: "submit"): void;
    (e: "update:visible", visible: boolean): void;
  };
}) {
  const { passData, type, formState, loading, formRef, emit } = state;
  const { resetFields } = FormHook({ formState });

  //提交表单
  const onSubmit = () => {
    formRef.value.validate().then(() => {
      const params = {
        ...formState
      };
      loading.value = true;

      combinedDeclarationApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };

  //取消运单
  const cancelSubmitHandle = () => {
    // Modal.confirm({
    //   title: "取消后将无法恢复，是否确认取消？",
    //   icon: createVNode(ExclamationCircleOutlined),
    //   onOk() {
    //     cancelCombinedApi(formState).then((res: any) => {
    //       const { code } = res;
    //       if (code === 200) {
    //         message.success("操作成功!");
    //         emit("submit");
    //       }
    //     });
    //   }
    // });
    loading.value = true;
    cancelCombinedApi(formState)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  //删除数组
  const deleteArr = (index: number) => {
    Modal.confirm({
      title: "是否删除当前运单号?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        formState.waybillNumList.splice(index, 1);
      }
    });
  };

  //重置表单
  const resetForm = () => {
    resetFields();
    emit("update:visible", false);
  };
  return {
    resetForm,
    onSubmit,
    deleteArr,
    cancelSubmitHandle
  };
}
